import {
  sysDepartUrl,
  sysRoleUrl,
  sysUserUrl,
  sysLogUrl, fileManager,baseDoc as baseDocUrl,
  baseLang
} from '../api/url'
import { getAction } from '../api/manage'

/**
 * 基础模块-开发配置
 * @type {*[]}
 */
let baseLangMenu = [
    {
      "routeFlag": "TRUE",
      "url": "/baseLong/module",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "模块列表",
      "icon": null,
      "menuType": "MENU",
      "component": "views/baseLang/ModuleList",
      "redirect": null,
      "children": []
    },
    {
      "routeFlag": "TRUE",
      "url": "/baseLong/statusCode",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "状态码",
      "icon": null,
      "menuType": "MENU",
      "component": "views/baseLang/StatusCodeList",
      "redirect": null,
      "children": []
    }
  ];
// 开发文档模块-开发配置
let baseDocMenu = [
  {
    "routeFlag": "TRUE",
    "url": baseDocUrl.url,
    "hidden": "FALSE",
    "keepAlive": "FALSE",
    "name": "在线文档",
    "icon": null,
    "menuType": "MENU",
    "component": "components/layouts/IframePageView",
    "redirect": null,
    "children": null,
  }
]
// 字典模块-开发配置
let dictMenu = {
  "routeFlag": "TRUE",
  "url": "/isystem/dict",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "数据字典",
  "icon": null,
  "menuType": "MENU",
  "component": "views/system/DictList",
  "redirect": null,
  "children": null
};
// 首页
let indexMenu = {
  "routeFlag": "TRUE",
  "url": "/index",
  "name": '首页',
  "meta": {title: '首页'},
  "component": 'views/Index',
  "icon":'setting',
  "hidden":'FALSE',
  "menuType": "MENU",
  "keepAlive": "FALSE",
  "componentRef":(resolve)=>require(['../views/Index'], resolve)
}

// core模块，包含权限，用户，角色，定时任务，日志-系统管理
let coreMenu = {
  "routeFlag": "TRUE",
  "url": "/isystem",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "系统管理",
  "icon": null,
  "menuType": "MENU",
  "component": "components/layouts/RouteView",
  "redirect": "/isystem/depart",
  "children": [
    {
      "routeFlag": "TRUE",
      "url": "/isystem/depart",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "机构管理",
      "icon": null,
      "menuType": "MENU",
      "component": "views/system/DepartList",
      "redirect": null,
      "children": [
        {
          url: [sysDepartUrl.add, sysDepartUrl.import].join(","),
          name: '新增部门',
          menuType: 'PERMISSION',
          perms: 'SYS:DEPART:ADD',
          operateType: 'ADD'
        },
        {
          url: sysDepartUrl.add,
          name: '新增顶级部门',
          menuType: 'PERMISSION',
          perms: 'SYS:DEPART_TOP:ADD',
          operateType: 'ADD'
        },
        {
          url: [sysDepartUrl.delete, sysDepartUrl.deleteBatch].join(","), name: '删除部门', menuType: 'PERMISSION',
          perms: 'SYS:DEPART:DEL', operateType: 'DELETE'
        },
        { url: sysDepartUrl.edit, name: '修改部门', menuType: 'PERMISSION', perms: 'SYS:DEPART:EDIT', operateType: 'EDIT' },
      ]
    },

    {
      "routeFlag": "TRUE",
      "url": "/isystem/role",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "角色管理",
      "icon": null,
      "menuType": "MENU",
      "component": "views/system/RoleList",
      "redirect": null,
      "children": [
        {
          url: [sysRoleUrl.add, sysRoleUrl.import].join(","),
          name: '新增角色',
          menuType: 'PERMISSION',
          perms: 'SYS:ROLE:ADD',
          operateType: 'ADD'
        },
        {
          url: [sysRoleUrl.delete, sysRoleUrl.deleteBatch].join(","),
          name: '删除角色',
          menuType: 'PERMISSION',
          perms: 'SYS:ROLE:DEL',
          operateType: 'DELETE'
        },
        {
          url: [sysRoleUrl.edit, sysRoleUrl.saveRolePermission, sysRoleUrl.grantUser].join(","),
          name: '修改角色',
          menuType: 'PERMISSION',
          perms: 'SYS:ROLE:EDIT',
          operateType: 'EDIT'
        },
      ]
    },
    {
      "routeFlag": "TRUE",
      "url": "/isystem/user",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "用户管理",
      "icon": null,
      "menuType": "MENU",
      "component": "views/system/UserList",
      "redirect": null,
      "children": [
        {
          url: [sysUserUrl.add, sysUserUrl.import].join(","),
          name: '新增用户',
          menuType: 'PERMISSION',
          perms: 'SYS:USER:ADD',
          operateType: 'ADD'
        },
        {
          url: [sysUserUrl.delete, sysUserUrl.deleteBatch].join(","),
          name: '删除用户',
          menuType: 'PERMISSION',
          perms: 'SYS:USER:DEL',
          operateType: 'DELETE'
        },
        {
          url: [sysUserUrl.edit, sysUserUrl.frozenBatch, sysUserUrl.changPassword].join(","),
          name: '编辑用户', menuType: 'PERMISSION', perms: 'SYS:USER:EDIT', operateType: 'EDIT'
        },

      ]
    },
    {
      "routeFlag": "TRUE",
      "url": "/isystem/permission",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "菜单管理",
      "icon": null,
      "menuType": "MENU",
      "component": "views/system/PermissionList",
      "redirect": null,
      "children": null,
    },
    {
      "routeFlag": "TRUE",
      "url": "/isystem/quartz",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "定时任务",
      "icon": null,
      "component":"views/system/QuartzJobList",
      "menuType": "MENU",
      "redirect": null,
      "children": null
    },
    {
      "routeFlag": "TRUE",
      "url": "/isystem/baseRegion",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "区域管理",
      "icon": null,
      "menuType": "MENU",
      "component": "views/system/BaseRegionList",
      "redirect": null,
      "children": []
    },
    {
      "routeFlag": "TRUE",
      "url": "/isystem/log",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "系统日志",
      "icon": null,
      "menuType": "MENU",
      "component": "views/system/LogList",
      "redirect": null,
      "children": [
        {
          url: [sysLogUrl.delete, sysLogUrl.deleteBatch].join(","),
          name: '删除日志',
          menuType: 'PERMISSION',
          perms: 'SYS:LOG:DEL',
          operateType: 'DELETE'
        },
        {
          url: sysLogUrl.cleanCache,
          name: '清空Url日志缓存',
          menuType: 'PERMISSION',
          perms: 'SYS:LOG:CLEAR_URL_INFO',
          operateType: 'EDIT'
        },
      ]
    }
  ]
}

//在线文件管理模块-系统管理
let fileSystemMenu={
  "routeFlag": "TRUE",
  "url": "/file",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "文件管理",
  "icon": null,
  "menuType": "MENU",
  "component": "components/layouts/RouteView",
  "redirect": fileManager.url,
  "children": [{
    "routeFlag": "TRUE",
    "url": fileManager.url,
    "hidden": "FALSE",
    "keepAlive": "FALSE",
    "name": "文件管理",
    "icon": null,
    "menuType": "MENU",
    "component": "components/layouts/IframePageView",
    "redirect": null,
    "children": null,
  }]
}


// 开发配置大菜单
let devConfigMenu = {
  "routeFlag": "TRUE",
  "url": "/devConfig",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "开发配置",
  "icon": null,
  "menuType": "MENU",
  "component": "components/layouts/RouteView",
  children:null,
};




 // 流程设计
let activitiMemu = {
  "routeFlag": "TRUE",
  "url": "/activiti",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "流程设计",
  "icon": "setting",
  "menuType": "MENU",
  "component": "components/layouts/RouteView",
  "redirect": "/activiti/model",
  "children": [
    {
      "routeFlag": "TRUE",
      "url": "/activiti/model",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "流程模型",
      "icon": null,
      "menuType": "MENU",
      "component": "views/activiti/ModelList",
      "redirect": null,
      "children":[]
    },
    {
      "routeFlag": "TRUE",
      "url": "/activiti/ProcessList",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "流程实例",
      "icon": null,
      "menuType": "MENU",
      "component": "views/activiti/ProcessList",
      "redirect": null,
      "children":[]
    },
    {
      "routeFlag": "TRUE",
      "url": "/activiti/MyTaskList",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "我的任务",
      "icon": null,
      "menuType": "MENU",
      "component": "views/activiti/MyTaskList",
      "redirect": null,
      "children":[]
    },
    {
      "routeFlag": "TRUE",
      "url": "/activiti/AllTaskList",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "所有任务",
      "icon": null,
      "menuType": "MENU",
      "component": "views/activiti/AllTaskList",
      "redirect": null,
      "children":[]
    },
    {
      "routeFlag": "TRUE",
      "url": "/activiti/HistoryProcessList",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "历史流程",
      "icon": null,
      "menuType": "MENU",
      "component": "views/activiti/HistoryProcessList",
      "redirect": null,
      "children":[]
    },
    {
      "routeFlag": "TRUE",
      "url": "/activiti/editModel",
      "hidden": "TRUE",
      "keepAlive": "FALSE",
      "name": "编辑模型",
      "icon": null,
      "menuType": "MENU",
      "component": "views/activiti/EditModelModal",
      "redirect": null,
      "children":[]
    },
  ]
}
// 服务监控
let monitorMenu = {
  "routeFlag": "TRUE",
  "url": "/monitor",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "服务器监控",
  "icon": null,
  "menuType": "MENU",
  "component": "components/layouts/RouteView",
  "redirect": "/monitor/disk",
  "children": [
    {
      "routeFlag": "TRUE",
      "url": "/monitor/disk",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "磁盘监控",
      "icon": null,
      "menuType": "MENU",
      "component": "views/monitor/DiskMonitoring",
      "redirect": null,
      "children":[]
    },

  // {
  //   "routeFlag": "TRUE",
  //   "url": "/monitor/redis",
  //   "hidden": "FALSE",
  //   "keepAlive": "FALSE",
  //   "name": "缓存监控",
  //   "icon": null,
  //   "menuType": "MENU",
  //   "component": "views/monitor/RedisInfo",
  //   "componentRef":(resolve)=>require(['../views/monitor/RedisInfo'], resolve),
  //   "redirect": null,
  //   "children": []
  // },
  // {
  //   "routeFlag": "TRUE",
  //   "url": "/monitor/http",
  //   "hidden": "FALSE",
  //   "keepAlive": "FALSE",
  //   "name": "请求监控",
  //   "icon": null,
  //   "menuType": "MENU",
  //   "component": "views/monitor/HttpTrace",
  //   "componentRef":(resolve)=>require(['../views/monitor/HttpTrace'], resolve),
  //   "redirect": null,
  //   "children": []
  //
  // },
  {
    "routeFlag": "TRUE",
    "url": "/monitor/jvm",
    "hidden": "FALSE",
    "keepAlive": "FALSE",
    "name": "JVM监控",
    "icon": null,
    "menuType": "MENU",
    "component": "views/monitor/JvmInfo",
    "redirect": null,
    "children": []

  },


  {
    "routeFlag": "TRUE",
    "url": "/monitor/system",
    "hidden": "FALSE",
    "keepAlive": "FALSE",
    "name": "系统监控",
    "icon": null,
    "menuType": "MENU",
    "component": "views/monitor/SystemInfo",
    "redirect": null,
    "children": null,

  },
  {
    "routeFlag": "TRUE",
    "url": "/monitor/tomcat",
    "hidden": "FALSE",
    "keepAlive": "FALSE",
    "name": "Tomcat监控",
    "icon": null,
    "menuType": "MENU",
    "component": "views/monitor/TomcatInfo",
    "redirect": null,
    "children": null
  },
  ],
}


let cmsMenu = {
  "routeFlag": "TRUE",
  "url": "/cms",
  "hidden": "FALSE",
  "keepAlive": "FALSE",
  "name": "网站设置",
  "icon": null,
  "menuType": "MENU",
  "component": "components/layouts/RouteView",
  "redirect": "/cms/info",
  "children": [
    {
      "routeFlag": "TRUE",
      "url": "/cms/info",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "网站信息",
      "icon": null,
      "menuType": "MENU",
      "component": "views/cms/CmsList",
      "redirect": null,
      "children":[]
    },

    {
      "routeFlag": "TRUE",
      "url": "/cms/menu",
      "hidden": "FALSE",
      "keepAlive": "FALSE",
      "name": "菜单管理",
      "icon": null,
      "menuType": "MENU",
      "component": "views/cms/CmsMenuList",
      "redirect": null,
      "children":[]
    },

  ],
}

let codegenMenu = [
  {
    "routeFlag": "TRUE",
    "url": "/codegen/entity",
    "hidden": "FALSE",
    "keepAlive": "FALSE",
    "name": "代码生成",
    "icon": null,
    "menuType": "MENU",
    "component": "views/codegen/CodeGenClassList",
    "redirect": null,
    "children": null
  },
];

export async function getDyRouterTable() {
  let res = await getAction(baseLang.module.list);
  let result = [];
  if (res.success) {
    let nos = new Set(res.result.map(t => t.no));
    result.push(indexMenu);

    if (nos.has('core')) {
      result.push(Object.assign({}, coreMenu));
    }
    if (nos.has('fileManager')) {
      result.push(Object.assign({}, fileSystemMenu));
    }

    if (nos.has("activiti")) {
      result.push(Object.assign({}, activitiMemu));
    }
    let devConfig = Object.assign({}, devConfigMenu);
    devConfig.children = [];
    if (nos.has('baseLang')) {
      devConfig.children = [...devConfig.children, ...baseLangMenu];
    }
    if (nos.has('baseDoc')) {
      devConfig.children = [...devConfig.children, ...baseDocMenu];
    }
    if (nos.has('baseDict')) {
      devConfig.children = [...devConfig.children, dictMenu];
    }
    if (nos.has('codegen')) {
      devConfig.children = [...devConfig.children, ...codegenMenu];
    }
    if (devConfig.children.length > 0) {
      devConfig.redirect = devConfig.children[0].url;
      result.push(devConfig);
    }

    if (nos.has("sys_monitor")) {
      result.push(Object.assign({}, monitorMenu));
    }
    if (nos.has("cms")) {
      result.push(Object.assign({}, cmsMenu))
    }
    return result;
  }else {
    throw new Error("获取模块失败");
  }
}

